package org.csu.small.utils;

import com.alibaba.fastjson.JSONObject;
import com.zhenzi.sms.ZhenziSmsClient;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Rambo
 * @date 2020-06-20 20:59
 */
public class MessageUtil {
    static String  apiUrl = "https://sms_developer.zhenzikj.com";
    static String appId = "106198";
    static String appSecret = "30282e00-0b28-42f4-b39a-8fa60eaee7de";
    static ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);

    public static boolean sendMessage(String number, String code, String messageId) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", number);
        params.put("templateId", "850");
        params.put("messageId", messageId);
        String[] templateParams = new String[2];
        templateParams[0] = code;
        templateParams[1] = "5分钟";
        params.put("templateParams", templateParams);
        String result = client.send(params);
        JSONObject jsonResult = JSONObject.parseObject(result);
        System.out.println(jsonResult);
        return Integer.parseInt(jsonResult.getString("code")) == 0;
    }
    public static String generateCode(){
        return Integer.toString((int)((Math.random()*9+1)*100000));
    }

    public static String balance() throws Exception {
        return client.balance();
    }
    public static void main(String[] args) throws Exception {
        String result = client.balance();
//        sendMessage("15243697484","123456","123456");
//        System.out.println(queryMessageId("123456"));
        System.out.println(result);
    }

    private static boolean queryMessageId(String messageId) throws Exception {
        String result = client.findSmsByMessageId(messageId);
        result = "{\n" +
                "\t\t\t    \"code\":0,\n" +
                "\t\t\t    \"data\":{}\n" +
                "\t\t\t}";
        JSONObject jsonResult = JSONObject.parseObject(result);
        System.out.println(jsonResult);
        return Integer.parseInt(jsonResult.getString("code")) == 0;
    }

}
